# 机器学习进阶-图像形态学操作-开运算与闭运算 1.cv2.morphologyEx(进行各类形态学变化) 2.op=cv2.MORPH_OPEN(先腐蚀后膨胀) 3.op=cv2.MORPH_CLOSE(先膨胀后腐蚀)
# https://www.cnblogs.com/my-love-is-python/p/10395049.html
import cv2
import matplotlib.pyplot as plt
import numpy as np

# 1.读取图片
img = cv2.imread('dige.png')

# 2. cv2.MORPH_OPEN 先进行腐蚀操作，再进行膨胀操作
kernel = np.ones((5, 5), np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

# 3. cv2.MORPH_CLOSE 先进行膨胀，再进行腐蚀操作
kernel = np.ones((5, 5), np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

images = [img, opening, closing]
titles = ['ORIGINAL', 'OPENING', 'CLOSING']

for i in range(3):
    plt.subplot(2, 3, i + 1)
    plt.imshow(images[i])
    plt.title(titles[i])
plt.show()